package persistence;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import model.TransactionAttribute;

public class TransactionAttributeDAO extends BaseDAO {
	
	public TransactionAttributeDAO() throws Exception{}
	
	public int deleteAllTransactionsAttribute() throws SQLException{
		int retorno;
		String sql = "DELETE FROM transaction_attribute";
		PreparedStatement ps = this.getConnection().prepareStatement(sql);
		retorno = ps.executeUpdate();
		closeConnection();
		return retorno;  
	}
	
	public boolean insertTransactionsAttribute(TransactionAttribute transactionAttribute, boolean closeConnection) throws SQLException{
		boolean retorno;
		String sql = "INSERT INTO transaction_attribute(" +
							"transaction_id, " +
							"attribute_id, " +
							"value " +
							") values (?,?,?)";
		PreparedStatement ps = this.getConnection().prepareStatement(sql);
		ps.setInt	( 1, transactionAttribute.getTransactionId());
		ps.setInt	( 2, transactionAttribute.getAttributeId());
		ps.setString( 3, transactionAttribute.getValue());
		retorno = ps.executeUpdate() == 1;
	    if(closeConnection){
	    	closeConnection();
	    }
	    return retorno;
	}
	
	public int insertListTransactionsAttribute(List<TransactionAttribute> listTransactionAttribute) throws Exception{
		int ret=0;
		for (int i = 0; i < listTransactionAttribute.size(); i++) {
			boolean foiInserido;
			foiInserido = insertTransactionsAttribute(listTransactionAttribute.get(i),false);
			if(foiInserido){
				ret++;
			}
		}
		closeConnection();
		return ret;
	}
}
